const model = require('./server/model')
const baseUrl=`https://ac.qq.com`
var Nightmare = require('nightmare');
let cheerio = require('cheerio')
let nightmare = Nightmare({
  show: true, width: 1920, height: 1080, waitTimeout: 10000, openDevTools: {
    mode: 'bottom'
  }
});

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

class Main {
  constructor(mainPageUrl) {
    this.mainPageUrl = mainPageUrl
    this.currentChapterIndex = 0;
    this.chapterCount = 2;
    this.currentUrl = 0
    this.pageUrl = ""
    this.chapterInfos = []
  }
  async getMainPage() {
    await nightmare.goto(this.mainPageUrl).wait('.chapter-page-pager');

    await nightmare.click('.chapter-page-btn-first').wait('.works-chapter-list a')
    this.chapterInfos =await nightmare.evaluate(()=>{
      return document.querySelector('#chapter').innerHTML
    }).then(html=>{
      const $ = cheerio.load(html)
      let chapterInfos = []
      let $chapters = $('.works-chapter-list a')
      console.log('len', $chapters.length)
      $chapters.each((i, dom) => {
        let href=$(dom).attr('href')
        chapterInfos.push({
          cid:href.substring(href.lastIndexOf('/')+1),
          title: $(dom).text(),
          url: baseUrl+href,
          num: i + 1,
          topic:'tlf'
        })
      })
      return chapterInfos
    })
    // this.chapterInfos = await nightmare.evaluate(baseUrl => {
    //   let chapterInfos = []
    //   let $chapters = $('.works-chapter-list a')
    //   console.log('len', $chapters.length)
    //   $chapters.each((i, dom) => {
    //     let href=$(dom).attr('href')
    //     chapterInfos.push({
    //       cid:href.substring(href.lastIndexOf('/')+1),
    //       title: $.trim($(dom).text()),
    //       url: baseUrl+href,
    //       num: i + 1,
    //       topic:'tlf'
    //     })
    //   })
    //   return chapterInfos
    // },baseUrl)

    // this.chapterInfos.forEach(async x=>{
    //   if(await model.ComicChapter.getBy(x)==null)
    //   await model.ComicChapter.create(x)
    // })
   
    await this.getChapter();
  }
  async getChapter(){
    console.log('getChapter')
    this.chapterInfos.forEach(async (x,i)=>{
      if(this.currentChapterIndex<1){
        console.log('currentChapterIndex',this.currentChapterIndex)
        this.currentChapterIndex++;
        await nightmare.goto(x.url).inject('js', 'jquery.min.js').wait('#comicContain li').wait(2000)
        let height=await nightmare.evaluate(()=>{
          return window.getComputedStyle(document.getElementById("comicContain")).height
        })
        height=parseInt(height.replace('px',''))-2000
        console.log(height)
        let contents= await nightmare.evaluate((cid)=>{
          let cts=[]
          $('#comicContain').scrollTop(2000);
          $('#comicContain li img')
          $('#comicContain li img').each((index,dom)=>{
            cts.push({
              cid,
              img_url:$(dom).attr('src'),
              width:$(dom).width(),
              height:$(dom).height(),
              num:index+1
            })
          })
          // console.log(window.getComputedStyle(document.getElementById("comicContain")).height)
          // document.querySelectorAll('#comicContain li img').forEach(dom=>{
          //   cts.push({
          //     cid,
          //     img_url:dom.src
          //   })
          // })
          return cts
        },x.cid)
        console.log(contents.length)
        
      }
    })
  }
}
let main = new Main(`${baseUrl}/Comic/comicInfo/id/540627`);

main.getMainPage();